package ar.edu.unq.tpi.uis.tepe3.wicket.components;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.hibernate.Session;
import org.hibernate.Transaction;

import ar.edu.unq.tpi.uis.tepe3.dominio.Folder;
import ar.edu.unq.tpi.uis.tepe3.persistencia.HibernateUtil;
import ar.edu.unq.tpi.uis.tepe3.wicket.pages.FolderPage;

@SuppressWarnings("serial")
public class FolderForm extends AbstractFormABM<Folder> {

	public FolderForm(String id, Folder object) {
		super(id, object);
	}

	@Override
	protected void addFields() {
		this.add(new TextField("name").setRequired(true));
		this.add(new DropDownChoice("father", this.getFatherChoices()));
	}

	@Override
	protected void redirectToListPage() {
		this.setResponsePage(FolderPage.class);
	}
	
	protected List<Folder> getFatherChoices(){
		List<Folder> list = new ArrayList<Folder>();
		Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = null;
		try {
			tx = sess.beginTransaction();
			list = sess.createCriteria(Folder.class).list();
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally{
			if(sess.isOpen()) sess.close();
		}
		return list;
	}

}
